<?php

function db_connect($name,$new_link=true)
{
    static $db;
    if(is_null($db))$db=require(APPLICATION_ROOT.'/config/mdb.cfg.php');
    if(!isset($db[$name]))exit("database '$name' not exist.");
    $link=mysql_connect(
        $db[$name]['host'].':'.(isset($db[$name]['port'])?$db[$name]['port']:'3306'),
        $db[$name]['user'],
        $db[$name]['passwd'],
        $new_link
    );
    if($link)return $link;
    else exit("database '$name' connect failed.\n");
}
function db_query($sql,$link,$file,$line)
{
    $result=mysql_query($sql,$link);
    if($result)return $result;
    else exit("file: $file\nline: $line\nsql:$sql\nerror: mysql_query ". mysql_error($link)."\n");
}
